package com.qf.pck.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.pck.entity.Package;
import com.qf.pck.entity.PackageTenantPerms;
import com.qf.pck.mapper.PackageMapper;
import com.qf.pck.mapper.PackageTenantPermsMapper;
import com.qf.pck.service.PackageService;
import com.qf.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 租户套餐表 服务实现类
 * </p>
 *
 * @author 程序员
 * @since 2023-10-28
 */
@Service
public class PackageServiceImpl extends ServiceImpl<PackageMapper, Package> implements PackageService {

    @Autowired
    private PackageTenantPermsMapper packageTenantPermsMapper;

    public R<List<Package>> listPck(){
        QueryWrapper<Package> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(Package::getStatus,0);
        List<Package> packages = this.baseMapper.selectList(queryWrapper);
        for (Package pck : packages) {
            String permIds = pck.getPermIds();
            final List<String> list = JSONUtil.toList(permIds, String.class);
            List<PackageTenantPerms> collect = list.stream().map(id->{
                return packageTenantPermsMapper.selectById(id);
            }).collect(Collectors.toList());
            pck.setPerms(collect);
        }
        return R.ok(packages);
    }

}
